<template>
    <div class="dahezi">
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item >🏠</el-breadcrumb-item>
            <el-breadcrumb-item>我的帖子</el-breadcrumb-item>
        </el-breadcrumb>
        <el-button  @click="dianji" type="success" >发帖</el-button>
        <div class="hezi">
            <div class="hezi2">
            <div class="hezi3">
                <!--                <div class="hezi3">-->
                <div class="aaa">
                    <!--                  内容-->
                        <div v-for="(p,index) in tiezii" :key="index">
                            <!--            帖子标题-->
                            <!--                一条帖子的框框，从这里开始v-for遍历-->
                            <div class="rediang1" >
                                <div id="rcorners1" >

                                    {{p.classificationName}}
                                </div>
                                <router-link :to="{name:'tiezixiangqing',
                                        query:{id:p.id,
                                        type:p.classificationName,
                                        content:p.content,
                                        time:p.time
                                        }}">
                                    <div class="neirong" @click="chakanxiangqing(p)">
                                        <div v-html="p.content.length > 100 ? p.content.slice(0, 100) + '...' :p.content" />
                                    </div>
                                </router-link>
                                <div class="huifucisu">
                                    回复({{p.count}})
                                </div>
                                <div class="huifucisu">
                                    <el-link type="danger" size="middle" @click="handleDelete(p.id)" icon="el-icon-delete">删除</el-link>
                                </div>
                            </div>
                        </div>
                </div>
                <!--                </div>-->
            </div>
            </div>
            </div>

        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="发帖人:" >
                    <el-tag type="info" effect="plain">{{form.userName}}</el-tag>
                    <!--                        <el-input v-model="form.userName" placeholder="请输入文章笔名" />-->
                </el-form-item>
                帖子分类:
                <el-radio-group v-model="form.classificationId" v-for="(p,index) in fengleibie" :key="index">
                    <el-radio :label="p.id">{{p.className}}</el-radio>
                </el-radio-group>
                <el-form-item label="帖子内容:">
                    <editor v-model="form.content" :min-height="192"/>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="tijiao">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import marked from "marked";
    import hljs from "highlight.js";
    import "highlight.js/styles/monokai-sublime.css";
    import Editor from '@/components/Editor';


    export default {
        name: "luntang",
        components: { Editor },
        data(){
            return{
                shiyisheng:'',
                form:{
                    userName:'',
                    userId:'',
                    content:'',
                    classificationId:''
                },
                tiezii:[],
                fengleibie:[],
                open:false
            }
        },
        mounted() {
            //初始化
            this.getList();
            marked.setOptions({
                renderer: new marked.Renderer(),
                highlight: function (code) {
                    return hljs.highlightAuto(code).value;
                },
                pedantic: false,
                gfm: true,
                tables: true,
                breaks: true,
                sanitize: false,
                smartLists: true,
                smartypants: false,
                xhtml: true,
            });
        },
        created() {
            this.getuserlll()
            this.fenglei()
        },
        methods:{
            chakanxiangqing(item){
                if(item.classificationId == 4){
                    this.$router.push({name: 'tiezixiangqing' , query: {id: item.fatherId}});
                }else{
                    this.$router.push({name: 'tiezixiangqing' , query: {id: item.id}});
                }

            },
            async getuserlll(){
                const {data:jieguo}=await this.$http.get('user/info',{headers:{
                        'Authorization':window.sessionStorage.getItem('token')
                    }})
                this.shiyisheng=jieguo.data.userRole
                this.form.userName=jieguo.data.basicUserInformation.username
                this.form.userId=jieguo.data.basicUserInformation.id
                this.basicUserInformation=jieguo.data.basicUserInformation
                this.tiezii=jieguo.data.topics
                // console.log( this.shiyisheng)
            },

            async fenglei(){
                const {data:jieguo}=await this.$http.get('/classification/list',{headers:{
                        'Authorization':window.sessionStorage.getItem('token')
                    }})
                this.fengleibie=jieguo.data
            },

            dianji(){
                    this.fenglei();
                    this.open=true;
            },
            tijiao() {
                if (!this.form.classificationId) {
                    this.$message('请选择分类');
                    return false
                }
                const token = sessionStorage.getItem("token");
                this.$http.defaults.headers.common['Authorization'] = token;
                this.$http.post('topic', this.form).then(response => {
                    console.log(response)
                    if (response.data.code === 200) {
                        this.$message({showClose: true, message: "发帖成功！", type: "success"})
                        this.open = false;
                        this.getuserlll()
                    }
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form.content=''
                this.form.classificationId=''
            },
            /** 删除按钮操作 */
            handleDelete(id) {
                this.$confirm('是否确认删除', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(async () => {
                    this.$http.delete(`topic/`+id).then(response => {
                        if (response.data.code == 200) {
                            this.$message({
                                showClose: true,
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getuserlll();
                        } else {
                            this.$message({
                                showClose: true,
                                message: response.data.msg,
                                type: response.data.code
                            });

                        }
                    });
                }).catch(() => {

                });
            },
        }
    }
</script>

<style scoped>

    .hezi{
        width: 80%;
        min-height: 900px;
        height: auto!important;
        display: flex;
        justify-content:center;
        align-items:center;

    }
    .hezi2{
        width: 100%;
        background-color: white;
        min-height: 850px;
        height: auto!important;
        margin-top: 3px;
        display: flex;
        /*justify-content:center;*/
    }
    .hezi3{
        width: 100%;
    }

    .dahezi{
        padding-top: 3px;
        background-color: #fffacd;
        min-height: 1000px;
        height: auto!important;
    }
    .aaa{
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
        overflow: scroll;
        min-height: 850px;
        width: 100%;

    }
    .yierhang{
        width: 800px;
        height: 100px;
        text-align: center;
        margin-left: 200px;
        margin-top: 20px;
        background: #FFFFE0;
        border: #FFFACD solid 2px;
    }
    .jinrirediang1{
        height: 150px;
        width: 40%;
        margin-bottom:25px;
    }

    .rediang1{
        border: #FFFACD solid 2px;
        background: #FFFFE0;
        margin-top: 20px;
        margin-left: 12%;
        width: 76%;
        height: 150px;
        overflow: hidden;
        text-overflow: ellipsis;
        color: #111111;
    }
    .neirong{
        height: 110px;
    }
    .huifucisu{
        height: 50px;
        width: 80px;
        float: right;
        color: black;
        /*background: #409EFF;*/
    }

    .el-button{
        margin-left: 50px;
    }
    #rcorners1 {
        border-radius: 100px;
        background: white;
        padding: 20px;
        width: 75px;
        height: 0px;
        float: right;
        text-align: center;
        color: black;
        font-size: 18px;
        border: #EAEDF1 solid 1px;
    }
    .el-dialog{
        background-color: #FFFFE0;
    }
    .dalog{
        display: flex;
        width: 650px;
        height: 250px;
        /*border: #EEE8AA solid 2px;*/
    }
    .doctouavatar{
        width: 200px;
        height: 200px;
        /*border: #888888 solid 2px;*/
    }
    .doctorjiesao{
        width: 150px;
        height: 150px;
        /*border: black solid 2px;*/
        margin-top: 50px;
        font-size: 20px;
        text-align: center;
    }
    .yaoqinganniu{
        width: 200px;
        height: 20px;
        float: bottom;
        /*border: black solid 2px;*/
    }
</style>
